//
//  MySafetyClaimsVC.m
//  AISHIDUNOA
//
//  Created by apple on 2017/6/12.
//  Copyright © 2017年 ronglian. All rights reserved.
//

#import "BaseWebViewVC.h"
#import <JavaScriptCore/JavaScriptCore.h>


@interface BaseWebViewVC ()<UIWebViewDelegate>

/** 网页*/
@property (nonatomic, strong) UIWebView       *webView;
/** 网页图片数组*/
@property (nonatomic, strong) NSMutableArray  *webImgsArr;
/** 申请订餐按钮*/
@property (nonatomic, strong) UIButton        *applyForMealBtn;
/** 问卷调查*/
@property (nonatomic, strong) UIButton        *moreBtn;

@end

@implementation BaseWebViewVC
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
 
}

- (void)dealloc {
    NSLog(@"dealloc");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
  
    //    [self createNavi];
    [self creatWebView];
    //    [self createBottomView];
}

/**
 *创建网页
 */
- (void)creatWebView {
    //    @"http://192.168.1.144:8035/farm/drone.html?rotate=0&space=10&polygon=23.130711038453224,113.26350152492525,23.13064690712129,113.26440274715425,23.12919161096194,113.26405942440034,23.1297243991846,113.2628685235977";
    
    if (!_webView) {
        _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0, 0, 0)];
        _webView.backgroundColor = [UIColor whiteColor];
        _webView.opaque = NO;
        _webView.delegate  = self;
        [self.view addSubview:_webView];
    }
//    NSLog(@"_latLngStr----%@",_latLngStr);
    //  [STANDARD_USER_DEFAULTS setObject:heightStr forKey:@"airCraftHeight"];
  NSString *flyHeight = [STANDARD_USER_DEFAULTS objectForKey:@"airCraftHeight"];
    
    [self reSetAirCraftLine:@"0" FlyHeight:flyHeight];
}

/**
 *加载网页
 */
-(void)reSetAirCraftLine:(NSString *)angleStr FlyHeight:(NSString *)flyHeight {
    CGFloat sideOverLay = [flyHeight floatValue] / 2;
    NSString *sideOverLaySt  = [NSString stringWithFormat:@"%f",sideOverLay];
    
    NSString *urlStr = [NSString stringWithFormat:@"http://p1hud0i6w.bkt.clouddn.com/drone/drone.html?rotate=%@&space=%@&polygon=%@",angleStr,sideOverLaySt,_latLngStr];
    
    NSURL *url = [NSURL URLWithString:urlStr];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [_webView loadRequest:request];
}

-(void)webViewDidFinishLoad:(UIWebView *)webView {
//    JSContext *context = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];

    NSString *str = [self.webView stringByEvaluatingJavaScriptFromString:@"getTitle();"];
    NSLog(@"JS返回值：%@",str);
    
    //航线节点返参
    if (self.airCraftLinePointsBlcok) {
        self.airCraftLinePointsBlcok(str);
    }
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    
    return YES;
}

- (void)createNavi
{
  
}

- (void)applyForMealBtnAction {
   
}

- (void)urlMoreBtnClick:(UIButton *)sender {
    
}

/**
 *申请理赔
 */
- (void)applyForSafeBtnClicked {
 
}

- (void)backClick {
    
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/**
 *弹出图退出代理
 */
- (void)funnyDetailViewBack
{
  
}

/**
 *复制访客信息
 */
- (void)saveInfo {
  
}

- (void)saveImageToPhotos:(UIImage *)image {
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:),nil);
}


-  (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    if (error == nil) {
        NSLog(@"保存成功");
        [ToolHelper notifyError:@"已保存到相册"];
    } else {
        NSLog(@"失败");
    }
}

/**
 *视图摇晃
 */
- (void)shakeAnimationForView:(UIView *) view {
    CALayer *viewLayer = view.layer;
    CGPoint position = viewLayer.position;
    CGPoint x = CGPointMake(position.x + 5, position.y);
    CGPoint y = CGPointMake(position.x - 5, position.y);
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
    [animation setFromValue:[NSValue valueWithCGPoint:x]];
    [animation setToValue:[NSValue valueWithCGPoint:y]];
    [animation setAutoreverses:YES];
    [animation setDuration:.06];
    [animation setRepeatCount:3];
    [viewLayer addAnimation:animation forKey:nil];
}
//获取当前时间
- (NSString *)getCurrentTime {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dateTime = [formatter stringFromDate:[NSDate date]];
    return dateTime;
}
-(int)compareDate:(NSString*)date01 withDate:(NSString*)date02{
    int ci;
    NSDateFormatter *df = [[NSDateFormatter alloc]init];
    [df setDateFormat:@"yyyy-MM-dd"];
    NSDate *dt1 = [[NSDate alloc]init];
    NSDate *dt2 = [[NSDate alloc]init];
    dt1 = [df dateFromString:date01];
    dt2 = [df dateFromString:date02];
    NSComparisonResult result = [dt1 compare:dt2];
    switch (result)
    {
            //date02比date01大
        case NSOrderedAscending:ci = 1;
            break;
            //date02比date01小
        case NSOrderedDescending:ci=-1;
            break;
            //date02=date01
        case NSOrderedSame:ci=0;
            break;
        default:
            NSLog(@"erorr dates %@, %@", dt2, dt1);
            break;
    }
    return ci;
}
@end

